<template>
  <div class="role-management">
    <!-- 搜索区域 -->
    <el-card class="search-card">
      <el-form :model="searchForm" inline>
        <el-form-item label="角色名称">
          <el-input v-model="searchForm.name" placeholder="请输入角色名称" clearable />
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="searchForm.description" placeholder="请输入描述" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch" icon="Search">搜索</el-button>
          <el-button @click="resetSearch" icon="Refresh">重置</el-button>
          <el-button type="success" @click="refreshData" icon="RefreshRight">刷新</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 操作区域 -->
    <el-card class="action-card">
      <el-button 
        v-if="hasPermission('system:role:create')"
        type="primary" 
        @click="showCreateDialog"
        icon="Plus"
      >
        新增角色
      </el-button>
    </el-card>

    <!-- 表格区域 -->
    <el-card>
      <el-table :data="tableData" v-loading="loading" stripe>
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="name" label="角色名称" width="150" />
        <el-table-column prop="displayName" label="角色" width="150" />
        <el-table-column prop="description" label="描述" min-width="200" />
        <el-table-column prop="createdAt" label="创建时间" width="180" />
        <el-table-column prop="updatedAt" label="更新时间" width="180" />
        <el-table-column label="操作" width="270" fixed="right">
          <template #default="{ row }">
            <el-button 
              v-if="hasPermission('system:role:edit')"
              type="primary" 
              size="small" 
              icon="Edit"
              @click="editRole(row)"
            >
              编辑
            </el-button>
            <el-button 
              v-if="hasPermission('system:role:assign_permission')"
              type="warning" 
              size="small" 
              icon="Sort"
              @click="assignPermissions(row)"
            >
              分配权限
            </el-button>
            <el-button 
              v-if="hasPermission('system:role:delete')"
              type="danger" 
              size="small" 
              icon="Delete"
              @click="deleteRole(row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        v-model:current-page="pagination.page"
        v-model:page-size="pagination.size"
        :total="pagination.total"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { usePermission } from '@/composables/usePermission'
import api from '@/utils/request'

const { hasPermission } = usePermission()

const loading = ref(false)
const tableData = ref([])

const searchForm = ref({
  name: '',
  description: ''
})

const pagination = ref({
  page: 1,
  size: 10,
  total: 0
})

const fetchData = async () => {
  loading.value = true
  try {
    const params = {
      page: pagination.value.page - 1,
      size: pagination.value.size,
      ...searchForm.value
    }
    
    const response = await api.get('/roles', { params })
    tableData.value = response.data.content || []
    pagination.value.total = response.data.totalElements || 0
  } catch (error) {
    ElMessage.error('获取角色列表失败')
  } finally {
    loading.value = false
  }
}

const handleSearch = () => {
  pagination.value.page = 1
  fetchData()
}

const resetSearch = () => {
  searchForm.value = {
    name: '',
    description: ''
  }
  handleSearch()
}

const refreshData = () => {
  fetchData()
}

const handleSizeChange = () => {
  pagination.value.page = 1
  fetchData()
}

const handleCurrentChange = () => {
  fetchData()
}

const showCreateDialog = () => {
  ElMessage.info('创建角色功能待实现')
}

const editRole = (role: any) => {
  ElMessage.info('编辑角色功能待实现')
}

const assignPermissions = (role: any) => {
  ElMessage.info('分配权限功能待实现')
}

const deleteRole = async (role: any) => {
  try {
    await ElMessageBox.confirm(`确定要删除角色 ${role.name} 吗？`, '确认删除', {
      type: 'warning'
    })
    
    await api.delete(`/roles/${role.id}`)
    ElMessage.success('删除成功')
    fetchData()
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('删除失败')
    }
  }
}

onMounted(() => {
  fetchData()
})
</script>

<style scoped>
.role-management {
  padding: 20px;
}

.search-card, .action-card {
  margin-bottom: 20px;
}
</style>


